﻿
Imports System.Threading
Imports System.Net
Imports System.Net.Sockets
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.procedure
Imports osi.root.constants
Imports osi.root.utils
Imports osi.root.lock
Imports osi.service.device

Partial Public Class connection_manager
    Public Shared Function [get](ByVal name As String,
                                 ByRef p As powerpoint) As Boolean
        Dim r As idevice_pool = Nothing
        Return device_manager.get(name, r) AndAlso
               cast(Of powerpoint)(r, p)
    End Function

    Public Shared Function [get](ByVal name As String) As powerpoint
        Dim r As powerpoint = Nothing
        assert([get](name, r))
        Return r
    End Function

    Public Shared Function expired(ByVal name As String) As Boolean
        Return device_manager.expired(name)
    End Function

    Public Shared Function connected_count(ByVal name As String) As Int64
        Return device_manager.total_count(name)
    End Function

    Public Shared Function free_connection_count(ByVal name As String) As Boolean
        Return device_manager.free_count(name)
    End Function

    Public Shared Function [get](ByVal name As String,
                                 ByRef r As TcpClient,
                                 Optional ByRef ppt As powerpoint = Nothing) As Boolean
        Return device_manager.sync_get(name, r, ppt)
    End Function

    Public Shared Function release(ByVal name As String, ByVal c As TcpClient) As Boolean
        Return device_manager.release(name, c)
    End Function

    Public Shared Function [get](ByVal name As String,
                                 ByVal c As pointer(Of TcpClient),
                                 Optional ByVal timeout_ms As Int64 = npos,
                                 Optional ByVal ppt As pointer(Of powerpoint) = Nothing) As event_comb
        Dim p As pointer(Of idevice_pool) = Nothing
        Dim ec As event_comb = Nothing
        Return New event_comb(Function() As Boolean
                                  p = New pointer(Of idevice_pool)()
                                  ec = device_manager.get(name, c, timeout_ms, p)
                                  Return waitfor(ec) AndAlso
                                         goto_next()
                              End Function,
                              Function() As Boolean
                                  Dim pt As powerpoint = Nothing
                                  Return ec.end_result() AndAlso
                                         cast(Of powerpoint)(+p, pt) AndAlso
                                         eva(ppt, pt) AndAlso
                                         goto_end()
                              End Function)
    End Function
End Class
